package com.tianqu.tms.core.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 列表头（有根据列排序功能）
 * @author liugang
 *
 */
public class SortTag extends TagSupport {

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -6328279409411520449L;
	
	//URL
	private String gotoURI;
	//一页显示的行数
	private String maxIndex = "12";
	//页面排序的title序号
	private String sortKey;
	//是否是降序
	private String asc;
	//table的列信息字符串（样式：项目1,项目2,项目3,项目4）
	private String titleList;
	//那些列需要有排序功能的标识字符串（样式：true,false,true,false）
	private String sortList;
	//每列的宽度字符串 （样式：20%,30%,*%,30%）
	private String widthList;
	//指定那一列是有多选功能,如果没有指定或者传值为0,那就表示没有一列是有多选功能，默认情况是0;
	private String checkBoxKey = "0";

	/**
	 * @throws JspException
	 */
	public int doStartTag() throws JspException {
		StringBuffer sb = new StringBuffer();
		//生成排序按钮的link
		if (gotoURI.indexOf("?") != -1) {
			gotoURI = gotoURI.concat("&");
		} else {
			gotoURI = gotoURI.concat("?");
		}
		gotoURI = gotoURI.concat("maxIndex="+maxIndex+"&pageNo=1&checkBoxKey="+checkBoxKey);
		//设置表头的html
		String titles[] = titleList.split(",");
		String sorts[] = sortList.split(",");
		String widths[] = widthList.split(",");
		if(titles.length == sorts.length && titles.length == widths.length) {
			//设定各列的宽度<col width="">
			sb.append("<colgroup>");
			for(int i=0;i<widths.length;i++) {
				sb.append("<col width=\""+widths[i]+"\" />");
			}
			sb.append("</colgroup>");
			//设定各列的头显示样式
			sb.append("<tr>");
			for(int i=0;i<titles.length;i++) {
				String title = titles[i];
				boolean sortValue = Boolean.valueOf(sorts[i]);
				//需要有多选功能
				if(i == (Integer.valueOf(checkBoxKey) - 1)) {
					sb.append("<th >");
					sb.append("<a href=\"javascript:;checkOnclickCom('tableDetail')\"><span id=\"ifquanxuan\">全选</span></a>");
					sb.append("<input type=\"hidden\" id=\"wp\" name=\"wp\" value=\"全选\"/>");
					sb.append("</th>");
					continue;
				} 
				if(sortValue) {
					sb.append("<th>");
					String gotoURI_tmp = gotoURI.concat("&sortKey=" + (i+1));
					if (i == (Integer.parseInt(sortKey) - 1)) {
						if(asc.equals("true")) {
							sb.append("<a href=\""+gotoURI_tmp.concat("&asc=false")+"\" class=\"sort-d-link-dark\"><strong>");
						} else {
							sb.append("<a href=\""+gotoURI_tmp.concat("&asc=true")+"\" class=\"sort-a-link-dark\"><strong>");
						}
					} else {
						sb.append("<a href=\""+ gotoURI_tmp.concat("&asc=true")+"\" class=\"sort-u-link-dark\"><strong>");
					}
					sb.append(title+"</strong></a></th>");
				}else {
					sb.append("<th>");
					sb.append(title);
					sb.append("</th>");
				}
			}
			sb.append("</tr>");
		}
		try {
			//标签样式输出
			pageContext.getOut().println(sb.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	public String getGotoURI() {
		return gotoURI;
	}
	public String getMaxIndex() {
		return maxIndex;
	}
	public String getSortKey() {
		return sortKey;
	}
	public String getAsc() {
		return asc;
	}
	public String getTitleList() {
		return titleList;
	}
	public void setGotoURI(String gotoURI) {
		this.gotoURI = gotoURI;
	}
	public void setMaxIndex(String maxIndex) {
		this.maxIndex = maxIndex;
	}
	public void setSortKey(String sortKey) {
		this.sortKey = sortKey;
	}
	public void setAsc(String asc) {
		this.asc = asc;
	}
	public void setTitleList(String titleList) {
		this.titleList = titleList;
	}
	public String getWidthList() {
		return widthList;
	}
	public void setWidthList(String widthList) {
		this.widthList = widthList;
	}
	public String getSortList() {
		return sortList;
	}
	public void setSortList(String sortList) {
		this.sortList = sortList;
	}
	public String getCheckBoxKey() {
		return checkBoxKey;
	}
	public void setCheckBoxKey(String checkBoxKey) {
		this.checkBoxKey = checkBoxKey;
	}
}
